package com.microboot.interceptor;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.springframework.stereotype.Component;

import java.util.List;

import org.apache.cxf.helpers.DOMUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.namespace.QName;

/**
 * 客户端登录拦截器
 * 业务描述：通过该拦截器来统一处理用户登录的包装处理
 */
@Component
public class ClientLoginInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

    private String username; //用户名
    private String password; //密码

    public ClientLoginInterceptor() {
        super(Phase.PRE_PROTOCOL);
    }

    public ClientLoginInterceptor(String username,String password) {
        super(Phase.PRE_PROTOCOL);
        this.username = username;
        this.password = password;
    }

    @Override
    public void handleMessage(SoapMessage soap) throws Fault {
        List<Header> headers = soap.getHeaders(); //获取全部头信息
        Document document = DOMUtils.createDocument(); //创建文档
        Element authority = document.createElement("authority"); //认证数据信息
        Element username = document.createElement("username");
        Element password = document.createElement("password");
        username.setTextContent(this.username);
        password.setTextContent(this.password);
        authority.appendChild(username);
        authority.appendChild(password);
        headers.add(0,new Header(new QName("authority"),authority));

    }
}
